<?php
/**
 * The base controller which is used by the Front and the Admin controllers
 */
class Base_Controller extends CI_Controller
{
	var $nodes	= '';
	var $pages = '';
	public function __construct(){
		 parent::__construct();
		
		//载入帮肋文件夹
		$this->load->helper('url');
		//判断关闭
		if($this->config->item('site_close')=='off'){
			show_error($this->config->item('site_close_msg'),500,'网站关闭');
		}
		//载入前台模板
		//$this->load->set_front_theme($this->config->item('themes'));
		$this->set_front_theme($this->config->item('themes'));
		//判断安装
		$file=FCPATH.'install.lock';
		if (!is_file($file)){
			//redirect(site_url('install'));
		}
		$this->load->database();
		
		//修改smarty目录
		//获取分级名
		$this->set_front_smarty_theme($this->config->item('themes'));
		
	   //获取get变量
		if($this->config->item('enable_query_strings')==FALSE){
			//取问号后面部分
			parse_str($_SERVER['QUERY_STRING'],$_GET);
		}
	 	//网站设定
// 		$data['items']=$this->db->get('settings')->result_array();
// 		$data['settings']=array(
// 			'site_name'=>$data['items'][0]['value'],
// 			'welcome_tip'=>$data['items'][1]['value'],
// 			'short_intro'=>$data['items'][2]['value'],
// 			'show_captcha'=>$data['items'][3]['value'],
// 			'site_run'=>$data['items'][4]['value'],
// 			'site_stats'=>$data['items'][5]['value'],
// 			'site_keywords'=>$data['items'][6]['value'],
// 			'site_description'=>$data['items'][7]['value'],
// 			'money_title'=>$data['items'][8]['value'],
// 			'per_page_num'=>$data['items'][9]['value'],
// 			'logo'=>$this->config->item('logo')
// 		 );

//         //用户相关信息
//         if ($this->session->userdata('uid')) {
// 	        $notices= $this->db->select('notices')->where('uid',$this->session->userdata('uid'))->get('users')->row_array();
// 	        $data['myinfo']=array(
// 				'uid'=>$this->session->userdata('uid'),
// 				'username'=>$this->session->userdata('username'),
// 				'avatar'=>$this->session->userdata('avatar'),
// 				'group_type'=>$this->session->userdata('group_type'),
// 				'gid'=>$this->session->userdata('gid'),
// 				'group_name'=>$this->session->userdata('group_name'),
// 				'is_active'=>$this->session->userdata('is_active'),
// 				'favorites'=>$this->session->userdata('favorites'),
// 				'follows'=>$this->session->userdata('follows'),
// 				'credit'=>$this->session->userdata('credit'),
// 				'notices'=>@$notices['notices'],
// 				'messages_unread'=>$this->session->userdata('messages_unread'),
// 				'lastpost'=>$this->session->userdata('lastpost')
// 	        );
//         }

		//获取二级目录
// 		$data['base_folder'] = $this->config->item('base_folder');

// 		//底部菜单(单页面)
// 		$this->load->model('page_m');
// 		$data['page_links'] = $this->page_m->get_page_menu(10,0);
// 		//模板目录
// 		$data['themes']=base_url('static/'.$this->config->item('themes').'/');
// 		//全局输出
// 		$this->load->vars($data);
	}	
	
	//设置vi模板路径
	public function set_front_theme($theme='default'){
		//引入分组
		
		$uri_protocol=$this->config->item('uri_protocol');
		if($uri_protocol=='PATH_INFO'){
			 //查有无index.php
			if(strpos($_SERVER['REQUEST_URI'],'/index.php')!==false){
				$url=explode("/",(str_replace("/index.php", '',$_SERVER['REQUEST_URI'])));
				//只有index.php 情况下
				if(count($url)<2){
					$url[1]='home';
				}else{
					if(!$url[1]){$url[1]='home';}
				}
			 }else{
			  	$url=explode("/",$_SERVER['REQUEST_URI']);
		   }
			//如果不存在
			$url[1]=$url[1] ? $url[1] : 'home';
			$this->load->set_front_theme($url[1]);
		}
	}
	
	//设置smarty模板路径
	public function set_front_smarty_theme($theme='default')
	{
	    //引入分组
	   
	   $uri_protocol=$this->config->item('uri_protocol');
	   if($uri_protocol=='PATH_INFO'){
	   	
	   
	   	//查有无index.php
	   	if(strpos($_SERVER['REQUEST_URI'],'/index.php')!==false){
	   		$url=explode("/",(str_replace("/index.php", '',$_SERVER['REQUEST_URI'])));
	        	if(count($url)<2){
					$url[1]='home';
				}else{
					if(!$url[1]){$url[1]='home';}
				}
	   		
	   	}else{
	   		$url=explode("/",$_SERVER['REQUEST_URI']);
	   		
	   	}
	   	//如果不存在
	   	$url[1]=$url[1] ? $url[1] : 'home'; 
	   	
	   	 $this->cismarty->template_dir=FCPATH.'themes/'.$url[1].'/';
	   	
	   }
		
		//echo $this->cismarty->template_dir;
	
	}
	
	public function assign($key,$val) {
		$this->cismarty->assign($key,$val);
	}
	
	public function display($html) {
		//print_r($this->cismarty);
		$this->cismarty->display($html);
	}
}
class Admin_Controller extends Base_Controller 
{
	function __construct()
	{
		parent::__construct();
		$this->load->database();
		//载入后台模板
		$this->load->set_admin_theme();
	 	//网站设定
		$data['items']=$this->db->get('settings')->result_array();
		$data['settings']=array(
			'site_name'=>$data['items'][0]['value'],
			'welcome_tip'=>$data['items'][1]['value'],
			'short_intro'=>$data['items'][2]['value'],
			'show_captcha'=>$data['items'][3]['value'],
			'site_run'=>$data['items'][4]['value'],
			'site_stats'=>$data['items'][5]['value'],
			'site_keywords'=>$data['items'][6]['value'],
			'site_description'=>$data['items'][7]['value'],
			'money_title'=>$data['items'][8]['value'],
			'per_page_num'=>$data['items'][9]['value']
		 );
		$this->load->vars($data);
	}
}
class Install_Controller extends Base_Controller 
{
	function __construct()
	{
		parent::__construct();
		//载入前台模板
		$this->load->set_front_theme('default');
	}
}
class Other_Controller extends Base_Controller 
{
	function __construct()
	{
		parent::__construct();
		$this->load->database();
		//载入前台模板
		$this->load->set_front_theme('default');
	}
}